
//工具
define(["core","angular"],function(core)
{
	var util=
	{
		//修改标题
		changeTitle:function(title)
		{
			if(!core.util.isBlank(title))
				title=title+" - "+core.System.APP_NAME;
			else
				title=core.System.APP_NAME;
			document.title=title;
		},
		//刷新
		refresh:function()
		{
			window.location.reload();
		},
		//修改地址
		changeLocation:function(url)
		{
			if(core.util.isBlank(url))
				url="#/";
			
			if(url.substring(0,1)!="#" && url.substring(0,4)!="http")
				url="#"+url;
			
			window.location.href=url;
		},
		//数据错误
		dataError:function(location)
		{
			location=core.util.defaultValue(location,true);
			core.ui.simpleDialog(core.message.message.ERROR_DATA_ERROR,function()
			{
				if(location)
				{
					util.changeLocation();
				}
			});
		},
		//返回引用路径
		getIncludePath:function(path)
		{
			return core.System.INCLUDE_PATH+path;
		},
		//默认回调
		defaultCallback:function(callback)
		{
			return core.util.defaultValue(callback,function(){});
		},
		//数据回调
		dataCallback:function(callback)
		{
			callback=util.defaultCallback(callback);
			return function(data)
			{
				if(core.util.isBlank(data))
					data=new Array();
			
				callback(data);
			};
		},
		//结果回调
		resultCallback:function(callback)
		{
			callback=util.defaultCallback(callback);
			return function(result)
			{
				result=core.message.parseResult(result);
				callback(result);
			};
		}
		,
		//分页回调
		pageCallback:function(callback)
		{
			callback=util.defaultCallback(callback);
			return function(data)
			{
				if(core.util.isBlank(data))
					data={data:[]};
				else
				{
					if(core.util.isBlank(data.data))
						data.data=[];
				}
				callback(data);
			};
		},
		baseRemove:function(items,item)
		{
			if(core.util.isBlank(items) || core.util.isBlank(item))
				return false;
			
			for(var i=0;i<items.length;i++)
			{
				if(items[i].id==item.id)
				{
					items.splice(i,1);
					return true;
				}
			}
			
			return false;
		},
		angular:
		{
			//返回引用模板标签
			getIncludeElement:function(url)
			{
				return "<div ng-include=\"'"+url+"'\" />";
			},
			dialog:
			{
				/**
				 * 创建标准弹出层
				 * 
				 * 	title 标题
				 * 	path 路径
				 * 	width 宽度
				 * 	height 高度
				 * 	showClose 是否显示关闭按钮
				 * 	moduleName 模块名称  默认随机生成临时模块
				 * 	controllerName 控制器名称
				 */
				baseDialog:function(option,callback)
				{
					var defaultOption=
					{
						title:"",
						path:"",
						width:null,
						height:null,
						showClose:true,
						moduleName:"TempModule_"+Math.round(Math.random()*10),
						controllerName:""
					};
					
					option=$.extend([],defaultOption,option);
					
					core.util.simpleGet(option.path,{},function(content)
					{
						var dialog=core.ui.dialog
						({
							title:option.title,
							content:content,
							width:option.width,
							height:option.height,
							showClose:option.showClose
						});
						
						var module=angular.module(option.moduleName,["directives","filters"]);
						module.controller(option.controllerName,["$scope",function($scope)
						{
							if(!core.util.isBlank(callback))
								callback($scope,dialog);
						}]);
						
						angular.bootstrap(dialog.getInstance(),[option.moduleName]);
					});
				},
				/**
				 * 创建弹出层模板
				 * @param controller
				 * 		func 创建的方法
				 * 		$scope 作用域
				 * 		item  增加项
				 * 		items
				 * 		controller 控制器 会在一开始执行
				 * 		handler 点击保存按钮执行
				 */
				baseCreate:function(option)
				{
					var defaultOption=
					{
						controller:
						{
							func:null,
							$scope:null,
							item:null,
							items:null,
							controller:function($createScope){},
							handler:function($createScope){},
							pushHandler:function($createScope,option,result){}
						}
					};
					defaultOption.controller.controller=function($createScope)
					{
						
					};
					defaultOption.controller.handler=function($createScope,dialog)
					{
						option.controller.func($createScope[option.controller.item],function(result)
						{
							if(result.success)
							{
								core.ui.simpleDialog(core.message.message.CREATE_SUCCESS);
								dialog.close();
								
								option.controller.pushHandler($createScope,option,result);
							}
							else
								core.ui.simpleDialog(result.message);
						});
					};
					defaultOption.controller.pushHandler=function($createScope,option,result)
					{
						option.controller.items.push(result.message);
						option.controller.$scope.$apply();
					};
					
					option.controller=$.extend([],defaultOption.controller,option.controller);
					util.angular.dialog.baseDialog(option,function($createScope,dialog)
					{
						option.controller.controller($createScope);
						
						$createScope.save=function()
						{
							option.controller.handler($createScope,dialog);
						};
						
						//取消
						$createScope.cancel=function()
						{
							dialog.close();
						};
					});
				},
				/**
				 * 编辑弹出层模板
				 * @param controller
				 * 		func 创建的方法
				 * 		$scope 作用域
				 * 		item  增加项
				 * 		items
				 * 		controller 控制器 会在一开始执行
				 * 		handler 点击保存按钮执行
				 */
				baseEdit:function(option)
				{
					var defaultOption=
					{
						controller:
						{
							func:null,
							$scope:null,
							source:null,
							item:null,
							items:null,
							controller:function($editScope){},
							handler:function($editScope){},
							updateHandler:function($editScope,option,result,source){}
						}
					};
					defaultOption.controller.controller=function($editScope)
					{
						
					};
					defaultOption.controller.handler=function($editScope,dialog)
					{
						var source=$editScope[option.controller.item];
						option.controller.func(source,function(result)
						{
							if(result.success)
							{
								dialog.close();
								core.ui.simpleDialog(core.message.message.UPDATE_SUCCESS);
								option.controller.updateHandler($editScope,option,result,source);
								
							}
							else
								core.ui.simpleDialog(result.message);
						});
					};
					defaultOption.controller.updateHandler=function($editScope,option,result,source)
					{
						var update=function(item)
						{
							if(core.util.isBlank(option.controller.items))
								return;
							for(var i=0;i<option.controller.items.length;i++)
							{
								if(option.controller.items[i].id==item.id)
								{
									option.controller.items[i]=item;
									option.controller.$scope.$apply();
									break;
								}
							}
						};
						
						update(source);
					};
					option.controller=$.extend([],defaultOption.controller,option.controller);
					util.angular.dialog.baseDialog(option,function($editScope,dialog)
					{
						$editScope[option.controller.item]=core.util.clone(option.controller.source);
						option.controller.controller($editScope);
						
						$editScope.save=function()
						{
							option.controller.handler($editScope,dialog);
						};
						
						//取消
						$editScope.cancel=function()
						{
							dialog.close();
						};
					});
				},
				/**
				 * 删除弹出层模板
				 * @param controller
				 * 		func 创建的方法
				 * 		$scope 作用域
				 * 		item  增加项
				 * 		items
				 * 		controller 控制器 会在一开始执行
				 * 		handler 点击保存按钮执行
				 */
				baseRemove:function(option)
				{
					var defaultOption=
					{
						message:"是否删除?",
						func:null,
						$scope:null,
						item:null,
						items:null,
						removeHandler:function(option){}
					};
					defaultOption.removeHandler=function(option)
					{
						if(util.baseRemove(option.items,option.item))
							option.$scope.$apply();
					};
					option=$.extend([],defaultOption,option);
					var checkDialog=core.ui.simpleDialogEx(option.message,function()
					{
						option.func(option.item.id,function(result)
						{
							core.ui.simpleDialog(result.message);
							if(result.success)
							{
								checkDialog.close();
								option.removeHandler(option);
							}
						});
					});
				}
			},
			route:
			{
				/**
				 *  解析路由
				 *  {
				 *  	url 路径
				 *  	templateUrl 模板路径
				 *  	controller 控制器名称
				 *  	default 是否为默认
				 *  }
				 */
				parse:function(providers,$routeProvider)
				{
					var isRoot=function(provider)
					{
						return (!core.util.isBlank(provider.url));
					};
					
					var handle=function(provider)
					{
						$routeProvider.when
						(
							provider.url,
							{
								templateUrl:provider.templateUrl,
								controller:provider.controller
							}
						);
						
						if(provider["default"]==true)
						{
							$routeProvider.otherwise
							({
								redirectTo:provider.url
							});
						}
					};
					
					for(var index in providers)
					{
						var provider=providers[index];
						
						if(isRoot(provider))
						{
							handle(provider);
						}
						else
						{
							util.angular.route.parse(provider,$routeProvider);
						}
					}
				}
			}
		}
	};
	
	return util;
});